package com.woniuxy.handler;

import com.woniuxy.common.Result;
import com.woniuxy.exception.parameter.ParameterValidateException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ParameterValidateException.class)
    public Result handleParameterValidateException(ParameterValidateException e){
        return Result.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){

        if (e instanceof MethodArgumentNotValidException){
            return Result.fail(5090,"参数错误");
        }
        else {
            e.printStackTrace();
            return Result.fail(500,e.getMessage());
        }

    }

}
